package com.sgb.kjwl.view.ui.fragment.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLeaseSearchCategoryBinding;
import com.sgb.kjwl.model.entity.LeaseSearchResultTypeEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 租赁搜索二级界面
 */
public class LeaseSearchCategoryFragment extends AppLazyFragment {

    private FragmentLeaseSearchCategoryBinding mBind;

    private List<LeaseSearchResultTypeEntity.CategoryBean> categoryBeanList;

    private String keyword;

    public static LeaseSearchCategoryFragment newInstance(List<LeaseSearchResultTypeEntity.CategoryBean> categoryBeanList,String keyword) {
        LeaseSearchCategoryFragment fragment = new LeaseSearchCategoryFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("categoryBeanList", (Serializable) categoryBeanList);
        bundle.putString("keyword",keyword);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_lease_search_category, container, false);
        return mBind.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() != null) {
            categoryBeanList = (List<LeaseSearchResultTypeEntity.CategoryBean>) getArguments().getSerializable("categoryBeanList");
            keyword = getArguments().getString("keyword");
        }
    }


    @Override
    protected void onLazyLoad() {
        showFirstCategory(categoryBeanList);
    }

    public void showFirstCategory(List<LeaseSearchResultTypeEntity.CategoryBean> data) {
        if (data == null || data.size() == 0) {
            DialogHelper.dismissProgressDialog();
            showEmpty();
            return;
        }
        ArrayList<Fragment> mFragments = new ArrayList<>();
        int size = data.size();
        String[] titles = new String[size];
        for (int i = 0; i < titles.length; i++) {//租赁类别
            mFragments.add(LeaseSearchItemFragment.newInstance(data.get(i).getType(),data.get(i).getRentType(),keyword));
            titles[i] = data.get(i).getName();
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), mFragments, titles);
        mBind.viewPager.setAdapter(mPagerAdapter);
        mBind.viewPager.setOffscreenPageLimit(0);
        mBind.tabLayout.setViewPager(mBind.viewPager);
    }

    public void showEmpty() {
        mBind.emptyView.setVisibility(View.VISIBLE);
    }
}
